Search this book | Previous | Table of contents | Next

Running scripts via AppleEvents


This section introduces how to write true CGI scripts through the use of AppleEvents sent by the server. This is the recommended method for executing scripts.

The use of AppleEvents to launch your scripts is the preferred method of running scripts. It eliminates the need for MacHTTP to load your scripts into RAM, prepend the global client variables, and most importantly, wait until the script is finished executing. Through the use of AppleEvents, much of this can be eliminated, a wider variety of scripting (programming) languages are available to you, and your scripts can run concurrently with the server application.

To make this happen using AppleScript as your scripting language you must:

  1. Include a handler in your script that waits for and processes AppleEvents
  2. Save your script as an application (not a text file) making sure the "Stay Open" and "Never Show Startup Screen" options are checked
  3. Give your script a .cgi extension rather than a .script extension
The "Hello, World" script (hello-world-05.cgi) is revisited here to illustrate this concept:
-- process the AppleEvent sent to this script by the server
on «event WWW*sdoc»
	
	-- define the standard HTTP header
	set LF to ASCII character (10)
	set CR to return
	set CRLF to CR & LF
	set http_10_header to "HTTP/1.0 200 OK" & CRLF & ¬
		"Server: MacHTTP" & CRLF & ¬
		"MIME-Version: 1.0" & CRLF & ¬
		"Content-type: text/html" & CRLF & CRLF
	
	-- return the results as an HTML file
	return http_10_header & ¬
		"<html>" & ¬
		"<head>" & ¬
		"<title>Hello, World</title>" & ¬
		"</head>" & ¬
		"<body>Hello, World!</body" & ¬
		"</html>"	
end «event WWW*sdoc»
This example, hello-world-05.cgi, illustrates one difference you can see and two differences you can not when compared to hello-world-02.script. The difference you can see is the addition of an AppleEvent handler. The AppleEvent handler is denoted by the addition of the on «event WWW*sdoc» and end «event WWW*sdoc» lines of code. This AppleEvent (WWW*sdoc) is sent by MacHTTP and WebSTAR and is used to initiate the script. It provides the means of passing the global client variables to the script as well.

Important! The AppleEvent is composed of two parts: "WWW" and "sdoc". These parts must be delimited by the omega character (option-z). The omega character does not display in HTML. Consequently, the asterisk (*) is used here instead. Every time you see the asterisk character in these scripts think the omega character.

The two differences you can not see entail how the script was saved. First, the script was saved as an application and not as a text file. Furthermore, the script was saved with the "Stay Open" and "Never Show Startup Screen" options checked.

Save As dialog box

Second, the script was given a name with a .cgi extension. This extension is defined in your server's configuration file and consequently it tells your server to treat any such file as a program to be executed.


Search this book | Previous | Table of contents | Next

Eric last edited this page on September 26, 1995. Please feel free to send comments.